/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.johnguo.base;

import java.beans.PropertyEditorSupport;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.johnguo.base.Page;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


/**
 * 控制器支持类
 * @author ThinkGem
 * @version 2013-3-23
 */
public abstract class BaseController {
	
	public Page bindPageData(HttpServletRequest request){
		
		String sEcho = ""; 			//分页相关
		String iDisplayStart = "";	//开始显示行   
		String iDisplayLength = "";	//每页显示的长度 
		String sSearch="";			//全局收索  
		String iSortCol_0="";		//排序列
		String sortColName = "";	//排序列的名称
		String sSortDir_0="";		//排序
		int s = 0;//开始行
		int e = 0;//显示记录
		
		Map<String, String> colMap = new HashMap<>();
		String jsondata = request.getParameter("aoData");   
		JSONArray jsonarray = JSONArray.fromObject(jsondata);
		if(jsondata!=null){
			for (int i = 0; i<jsonarray.size(); i++) {       
				JSONObject obj = (JSONObject) jsonarray.get(i); 
			   if (obj.get("name").equals("sEcho"))     
				   sEcho = obj.get("value").toString();     
			   if (obj.get("name").equals("iDisplayStart"))     //开始显示行      
				   iDisplayStart = obj.get("value").toString();     
			   if (obj.get("name").equals("iDisplayLength"))     //每页显示的长度      
				   iDisplayLength = obj.get("value").toString(); 
			   if (obj.get("name").equals("sSearch"))     //全局收索      
				   sSearch = obj.get("value").toString(); 
			   if (obj.get("name").equals("iSortCol_0"))     //排序列     
				   iSortCol_0 = obj.get("value").toString(); 
			   if (obj.get("name").equals("sSortDir_0"))     //排序     
				   sSortDir_0 = obj.get("value").toString(); 
			   //列名存放到map中
			   if(obj.get("name").toString().startsWith("mDataProp_")){
				   colMap.put(obj.get("name").toString(),  obj.get("value").toString());
			   }
			}
			
			s = Integer.parseInt(iDisplayStart);
			e = Integer.parseInt(iDisplayStart)+Integer.parseInt(iDisplayLength);
		}
		
		Page page = Page.newBuilder(Integer.parseInt(iDisplayStart)+1,Integer.parseInt(iDisplayLength),null);  
		page.setPageNo(Integer.parseInt(iDisplayStart)/Integer.parseInt(iDisplayLength)+1);
		
		page.setSortDir(sSortDir_0);
		if (StringUtils.isNotBlank(iSortCol_0)) {
			int colIndex = Integer.parseInt(iSortCol_0);
			String colName = colMap.get("mDataProp_" + colIndex);
			if (StringUtils.isNotBlank(colName)) {
				page.setSortColName(colName);
			}
		}
		return page;
	}
	
	public Page bindPage(HttpServletRequest request){
		
		int pageNo = Integer.parseInt(request.getParameter("page"));
		int pageSize = Integer.parseInt(request.getParameter("rows"));
		Page page = Page.newBuilder(pageNo,pageSize,null);  
			
		return page;
	}
	
	public String toPageJson(List<Map<String,Object>> result, Page page){
		String json =  "{\"iTotalRecords\":"+page.getTotalRecord()+",\"iTotalDisplayRecords\":"+page.getTotalRecord()+",\"aaData\":"+JSONArray.fromObject(result)+"}";  
		return json;
	}
}
